package com.yihu.cms.action;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.yihu.beans.ResultVO;
import com.yihu.beans.user.PermitQuery;
import com.yihu.beans.user.PermitResourceRef;
import com.yihu.beans.user.Priviledge;
import com.yihu.cms.service.PriviledgeService;

@Controller
public class PriviledgeController extends BaseController{
	

	private PriviledgeService ps = new PriviledgeService();
	@RequestMapping(value = "/user/priviledge" , method = RequestMethod.POST)
	public @ResponseBody Object addPriviledge(@RequestBody Priviledge p) {
		log.debug("addPriviledge: "+p.toString());
		ResultVO result= ps.addPermit(p);
		log.debug(JSON.toJSONString(result));
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value = "/user/priviledge/{id}", method = RequestMethod.PUT)
	public @ResponseBody Object updatePriviledge(@RequestBody Priviledge p,@PathVariable("id") long id) {
		p.setPermitId(id);
		log.debug("updatePriviledge: "+p.toString());
		ResultVO result= ps.updatePermit(p);
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value = "/user/priviledge/{id}", method = RequestMethod.DELETE)
	public @ResponseBody Object deletePriviledge(@PathVariable("id") long id) {
		log.debug("deletePriviledge: "+id);
		ResultVO result= ps.deletePermit(id);
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value = "/user/priviledge/{id}", method = RequestMethod.GET)
	public @ResponseBody Object getPriviledge(@PathVariable("id") long id) {
		log.debug("getPriviledge id: "+id);
		Priviledge p = ps.getPermit(id);
		return JSON.parseObject(JSON.toJSONString(p), Priviledge.class);
	}
	
	@RequestMapping(value = "/user/priviledge/list" , method = RequestMethod.GET)
	// , method = RequestMethod.PATCH
	public @ResponseBody Object getPriviledgesPaging(PermitQuery query) {
		log.debug("getPriviledgesPaging "+query.toString());
		String resultJson= ps.getPermitByPage(query);
		return JSON.parse(resultJson);
	}
	
	@RequestMapping(value = "/user/priviledge/addResource" , method = RequestMethod.POST)
	public @ResponseBody Object addResourceToPriviledge(@RequestBody PermitResourceRef p) {
		log.debug("addPriviledge: "+p.toString());
		ResultVO result= ps.addResourceToPriviledge(p);
		log.debug(JSON.toJSONString(result));
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value = "/user/priviledge/deleteResource" , method = RequestMethod.POST)
	public @ResponseBody Object deleteResourceFromPriviledge(@RequestBody PermitResourceRef p) {
		log.debug("deleteResourceFromPriviledge: "+p.toString());
		ResultVO result= ps.deleteResourceFromPriviledge(p);
		log.debug(JSON.toJSONString(result));
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value = "/user/priviledge/assignResources" , method = RequestMethod.POST)
	public @ResponseBody Object assignResources(@RequestBody PermitResourceRef p) {
		log.debug("assignResources: "+p.toString());
		ResultVO result= ps.assignResources(p);
		log.debug(JSON.toJSONString(result));
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value = "/user/priviledge/resources/{id}", method = RequestMethod.GET)
	public @ResponseBody Object getResources(@PathVariable("id") long id) {
		log.debug("getResources id: "+id);
		PermitQuery query = new PermitQuery();
		query.setPermitId(id);
		Object o = ps.getResourcesByPermit(query);
		return JSON.parse(JSON.toJSONString(o));
		
	}
	//通过parentId获取所有子权限，id为0为最上层权限
	@RequestMapping(value = "/user/priviledges/{id}", method = RequestMethod.GET)
	public @ResponseBody Object getPermits(@PathVariable("id") long id) {
		log.debug("getResources id: "+id);
		PermitQuery query = new PermitQuery();
		query.setPermitId(id);
		Object o = ps.getResourcesByPermit(query);
		return JSON.parse(JSON.toJSONString(o));
		
	}
	
	//通过parentId获取当前父权限下面的子权限，id为0为最上层权限
	@RequestMapping(value = "/user/priviledges/parent/{id}", method = RequestMethod.GET)
	public @ResponseBody Object getParentPermits(@PathVariable("id") long id) {
		log.debug("getParentPermits : "+id);
		PermitQuery query = new PermitQuery();
		query.setPermitId(id);
		Object o = ps.getParentPermits(query);
		return JSON.parse(JSON.toJSONString(o));
		
	}
	
	//通过parentId获取所有子权限，包括子权限下的子权限
	@RequestMapping(value = "/user/priviledge/childrens/{id}", method = RequestMethod.GET)
	public @ResponseBody Object getChildrensByParentId(@PathVariable("id") long id) {
		log.debug("getChildrensByParentId : "+id);
		Object o = ps.getChildrensByParentId(id);
		return JSON.parse(JSON.toJSONString(o));
		
	}
	
	

}
